_snsLoginConn = "";
//登录表单提交
function loginSubmit() {
	var viewMsg = jQuery("#viewMsg");
	var loginId = jQuery("#loginId");
	if (loginId.val() == null || loginId.val() == "" || loginId.val() == "用户名或Email帐号") {
		alert("请输入用户名,然后再登陆");
		loginId.focus();
		return false;
	}
	var pwd = jQuery("#password");
	if (pwd.val() == null || pwd.val() == "") {
		alert("请输入密码,然后再登陆");
		pwd.focus();
		return false;
	}

	var code = jQuery("#validateCode");
	if (code.val() == null || code.val() == "") {
		alert("请输入验证码,然后再登陆");
		code.focus();
		return false;
	}
	return true;
}

function loginIfrm() {
	var msg = loginIfram.document.body.innerHTML;
	loginResult(msg);
}
//分析登录返回代码
function loginResult(msg) {
	var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1; //google chrome
	var aData = msg.split(",");
	if (isChrome) {
		if (aData.length > 1 && aData[0] == 0) {
			var url = aData[1];
			_snsLoginConn = aData[6];
			var returnUrl = aData[7] == "" ? "" : "?returnUrl=" + aData[7];
			//setLastUser();
			if (_snsLoginConn == "yes") {
				jQuery.getJSON(_snsUrl + "emall/setLogin.jsp?frontEnd_userId=" + aData[3] + "&frontEnd_loginId=" +
					aData[4] + "&frontEnd_time=" + aData[5] + "&isAdmin=no&emallUrl=" + encodeURIComponent(url +
						returnUrl) + "&encode=" + aData[8] + "&jsoncallback=?",
					function(data) {});
				window.location = url + returnUrl;
				//window.location.href=_snsUrl+"emall/setLogin.jsp?frontEnd_userId="+aData[3]+"&frontEnd_loginId="+aData[4]+"&frontEnd_time="+aData[5]+"&isAdmin=no&emallUrl="+encodeURIComponent(url+returnUrl)+"&encode="+aData[8];
			} else {
				window.location = url + returnUrl;
			}
		} else {
			if (aData[0] == 1) {
				alert("您输入的帐号不存在，请重新输入！");
				jQuery("#loginId").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 2) {
				alert("您的帐号或密码输入错误，请重新输入！");
				jQuery("#loginId").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 3) {
				alert("验证码输入错误，请重新输入！");
				jQuery("#validateCode").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 4) {
				alert("卡号不存在");
				jQuery("#loginId").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 5) {
				alert("请输入用户名,然后再登陆");
				jQuery("#loginId").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 6) {
				alert("请输入密码,然后再登陆");
				jQuery("#password").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 7) {
				alert("请输入验证码,然后再登陆");
				jQuery("#validateCode").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 8) {
				alert("帐号被冻结，请与管理员联系");
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 9) {
				alert("账号需要管理员审核通过，请等待管理员审核通过！");
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}
			if (aData[0] == 10) {
				alert("大客户不能在此登录");
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
			}

		}
	} else {
		switch (aData[0]) {
			case "0":
				var url = aData[1];
				_snsLoginConn = aData[6];
				var returnUrl = aData[7] == "" ? "" : "?returnUrl=" + aData[7];
				//setLastUser();
				if (_snsLoginConn == "yes") {
					jQuery.getJSON(_snsUrl + "emall/setLogin.jsp?frontEnd_userId=" + aData[3] + "&frontEnd_loginId=" +
						aData[4] + "&frontEnd_time=" + aData[5] + "&isAdmin=no&emallUrl=" + encodeURIComponent(url +
							returnUrl) + "&encode=" + aData[8] + "&jsoncallback=?",
						function(data) {});
					window.location = url + returnUrl;
					//window.location.href=_snsUrl+"emall/setLogin.jsp?frontEnd_userId="+aData[3]+"&frontEnd_loginId="+aData[4]+"&frontEnd_time="+aData[5]+"&isAdmin=no&emallUrl="+encodeURIComponent(url+returnUrl)+"&encode="+aData[8];
				} else {
					window.location = url + returnUrl;
				}
				break;
			case "1":
				alert("您输入的帐号不存在，请重新输入！");
				jQuery("#loginId").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "2":
				alert("您的帐号或密码输入错误，请重新输入！");
				jQuery("#loginId").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "3":
				alert("验证码输入错误，请重新输入！");
				jQuery("#validateCode").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "4":
				alert("卡号不存在");
				jQuery("#loginId").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "5":
				alert("请输入用户名,然后再登陆");
				jQuery("#loginId").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "6":
				alert("请输入密码,然后再登陆");
				jQuery("#password").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "7":
				alert("请输入验证码,然后再登陆");
				jQuery("#validateCode").focus();
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "8":
				alert("帐号被冻结，请与管理员联系");
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "10":
				alert("大客户不能在此登录");
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
			case "9":
				alert("账号需要管理员审核通过，请等待管理员审核通过！");
				jQuery("#password").val("");
				jQuery("#validateCode").val("");
				vCodeRefresh();
				break;
		}
	}
}
//刷新验证码
function vCodeRefresh() {
	jQuery("#ValidateCode").attr("src", frontPath + "/ValidateCode?dumy=" + Math.random());
}

function changeMsg(obj) {
	if (obj.value.length >= 0 || obj.value != "") {
		alert("(请输入您注册时的用户名/E-MAIL地址)");
	}
}

function clean(id) {
	document.getElementById(id).value = '';
	// document.getElementById(id).className='';
	jQuery("#" + id).css("color", "#333333");
}

function review(id) {
	if (document.getElementById(id).value == '') {
		document.getElementById(id).value = '用户名或Email帐号';
		jQuery("#" + id).css("color", "#ccc");
	}
}

function showTab(id) {
	jQuery("#" + id).css("display", "block");
}

function hideTab(id) {
	jQuery("#" + id).css("display", "none");
}

var login_cookie_id = '49BAC005-7D5B-4231-8CEA-16939BEACD67';

function initLogin() {
	document.getElementById('loginId').value = '用户名或Email帐号';
	getLastUser();
}

function getLastUser() {
	var usr = getLastUserName();
	if (usr != null) {
		jQuery("#loginId").val(usr);
		jQuery("#loginId").css("color", "#333333");
		jQuery("#password").focus();
	} else {
		jQuery("#loginId").val('用户名或Email帐号');
		jQuery("#loginId").focus();

	}
}

function getLastUserName() {
	return getCookie(login_cookie_id);
}

function setLastUser() {
	//取用户名
	var usr = document.getElementById('loginId').value;
	var expdate = new Date();
	//当前时间加上两周的时间
	expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
	setCookie(login_cookie_id, usr, expdate, '/');
}

function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		//alert(j);
		if (unescape(document.cookie.substring(i, j)) == arg) return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//写入到Cookie

function setCookie(name, value, expires, path, domain, secure) {
	var argv = arguments;
	var argc = arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + (
		(path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure ==
		true) ? "; secure" : "");
}
